//
//  UIViewController+HDJihuoController.m
//  app
//
//  Created by pro on 2017/4/13.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDJihuoController.h"
#import "HDJihuoView.h"
#import "HDLockManger.h"
#import "HDEditDeleteSheet.h"

@interface HDJihuoController ()<HDJihuoViewDelegate>
{
    HDJihuoView * jihuo_view;
 //   HDEditDeleteSheet * sheet;
}

@property (nonatomic,strong) HDLockManger * manger;
@property (nonatomic,strong) HDJiHuoRequset * jihuo_reqeust;
@property (nonatomic,strong) HDLockRequset * login_reqeust;
@property (nonatomic,strong) HDMsgCodeRequset * msg_reqeust;
@property (nonatomic,strong) HDMsgCodeRequset * ji_msg_reqeust;
@property (nonatomic,strong) NSTimer * timer;

@end

@implementation  HDJihuoController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"门禁激活";
    
    self.view.backgroundColor = [XCommon hexStringToColor:kColor_brand];
    
    self.manger = [HDLockManger shareManager];
    self.jihuo_reqeust = [[HDJiHuoRequset alloc]initWithTarget:self action:@selector(jihuoResult:)];
    self.login_reqeust = [[HDLockRequset alloc]initWithTarget:self action:@selector(loginOutResult:)];
    self.msg_reqeust = [[HDMsgCodeRequset alloc]initWithTarget:self action:@selector(msgResutl:)];
    self.ji_msg_reqeust = [[HDMsgCodeRequset alloc]initWithTarget:self action:@selector(jiMsgresult:)];
    
    
    jihuo_view = [[HDJihuoView alloc]initWithFrame:CGRectMake(0, 64, UBO_SCREEN.width, UBO_SCREEN.height)];
    jihuo_view.delegate = self;
    [self.view addSubview:jihuo_view];
    
    
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
    i = 60;
    
    
}


static int i = 60;
-(void)goSendMsg
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * params = @{@"phone":jihuo_view.tf_3.text};
    
    [self.manger getMsgCodeRequest:self.msg_reqeust Params:params];
    
    
}
-(void)msgResutl:(HDMsgCodeRequset*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success) {
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange:) userInfo:nil repeats:YES];
        
    }else
    {
        
        [MyToast showWithText:rt.msg];
        jihuo_view.send_yanzheng.enabled = YES;
        [jihuo_view.send_yanzheng setTitle:@"发送验证码" forState:UIControlStateNormal];
    }
    
}
-(void)timeChange:(NSTimer*)timer
{
    if (i > 0 ) {
        [jihuo_view.send_yanzheng setTitle:[NSString stringWithFormat:@"%d秒",i--] forState:UIControlStateNormal];
    }else
    {
        [jihuo_view.send_yanzheng setTitle:@"发送验证码" forState:UIControlStateNormal];
        jihuo_view.send_yanzheng.enabled = YES;
        [self.timer invalidate];
        self.timer = nil;
        i = 60;
    }
    
    
    
    
}


-(void)goChongXinYanZheng
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * params = @{@"phone":jihuo_view.tf_3.text,
                              @"code":jihuo_view.tf_4.text,
                              @"phone_sn":KEY_CHAIN_UDID};
    [self.manger loginRequst:self.login_reqeust Params:params];
    
    
}
-(void)loginOutResult:(HDLockRequset*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success) {
        [MyToast showWithText:rt.msg];
        
        [[NSUserDefaults standardUserDefaults]setObject:rt.pushDic[@"result"] forKey:@"linglingID"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        //利用block 跳转回主页
        if (self.DenglublockJump) {
            self.DenglublockJump();
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else
    {
        [MyToast showWithText:rt.msg];
    }
}


-(void)goJIhuo
{
    
    
    
    
    //点击后，先调用发送验证码接口，发送成功后，弹出弹窗，输入验证码后，在条用激活接口
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
//    NSDictionary * params = @{@"phone":jihuo_view.tf_2.text};
//    
//    [self.manger getMsgCodeRequest:self.ji_msg_reqeust Params:params];
    
    [self jihuoPhone];
    
    
}
-(void)jiMsgresult:(HDMsgCodeRequset*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success) {
        
        __weak typeof(self) wself = self;
        HDEditDeleteSheet * sheet = [[HDEditDeleteSheet alloc]initWithParams:nil Sign:nil];
        sheet.msgCodeBlock = ^(NSString * msg_code){
        
//            [wself jihuoPhone:msg_code];
            
        };
        [sheet showEditDeleteActionSheet];
        
    }else
    {
        [MyToast showWithText:rt.msg];
    }

}



-(void)jihuoPhone
{
    
//    [[NSUserDefaults standardUserDefaults]setObject:msg_code forKey:@"lock_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    NSDictionary * params = @{@"active_sn":jihuo_view.tf_1.text,
                              @"phone":jihuo_view.tf_2.text,
                              @"phone_sn":KEY_CHAIN_UDID};
    [self.manger getJiHuoRequest:self.jihuo_reqeust Params:params];
    
    
    
}
-(void)jihuoResult:(HDJiHuoRequset*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success) {
        [MyToast showWithText:rt.msg];
        //利用block 跳转回主页
        if (self.JIhuoblockJump) {
            self.JIhuoblockJump();
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else
    {
        [MyToast showWithText:rt.msg];
    }
    
    
}

-(void)begainEdit
{
    //NSDictionary *info = [notification userInfo];
    
    //取出动画时长
    CGFloat animationDuration = 1.0f;
    
    //取出键盘位置大小信息
  //  CGRect keyboardBounds = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //rect转换
    //CGRect keyboardRect = [self.view convertRect:keyboardBounds toView:nil];
    
    
    //记录Y轴变化
  //  CGFloat keyboardHeight = keyboardBounds.size.height;
    
    //上移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)0 << 16;
    
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        //        self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight + 64);
        jihuo_view.frame = CGRectOf([self.view.ubo fx:0 fy:0 oy:-200], [self.view.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height-64]);
      //  self.bottomBar.frame = CGRectOf([self.view.ubo fx:0 fy:1 oy:-keyboardHeight-44], [self.view.ubo ow:UBO_SCREEN.width oh:44]);
        
    } completion:nil];
}

-(void)endEdit
{
    //NSDictionary *info = [notification userInfo];
    
    //取出动画时长
    CGFloat animationDuration = 1.0f;
    
    //下移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)0 << 16;
    
    //回复动画
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        //        self.table.transform = CGAffineTransformIdentity;
        jihuo_view.frame = CGRectOf([self.view.ubo fx:0 fy:0 oy:64], [self.view.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height -64]);
        //self.bottomBar.frame = CGRectOf([self.view.ubo fx:0 fy:1 oy:-44], [self.view.ubo ow:UBO_SCREEN.width oh:44]);
    } completion:nil];
    
}

@end
